package com.leyou.item.service;

import com.leyou.item.mapper.SpecGroupMapper;
import com.leyou.item.mapper.SpecParamMapper;
import com.leyou.item.mapper.SpecificationMapper;
import com.leyou.item.pojo.SpecGroup;
import com.leyou.item.pojo.SpecParam;
import com.leyou.item.pojo.Specification;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @Description:
 * @author: Mr_Sun
 * @Date: 2018/9/6
 * @Time: 11:10
 */
@Service
public class SpecificationService {

    @Autowired
    private SpecificationMapper specificationMapper;
    @Autowired
    private SpecGroupMapper specGroupMapper;
    @Autowired
    private SpecParamMapper specParamMapper;

    public void saveTemplate(Specification specification) {
        specificationMapper.insert(specification);

    }

    public void updateTemplate(Specification specification) {
        specificationMapper.updateByPrimaryKeySelective(specification);
    }

    public List<SpecGroup> querySpecGroups(Long cid) {
        Example example = new Example(SpecGroup.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("cid", cid);

        List<SpecGroup> list = specGroupMapper.selectByExample(example);

        return list;
    }

    public List<SpecParam> querySpecParam(Long gid, Long cid, Boolean searching, Boolean generic) {
        SpecParam param = new SpecParam();
        param.setGroupId(gid);
        param.setCid(cid);
        param.setSearching(searching);
        param.setGeneric(generic);
        return this.specParamMapper.select(param);

    }

    public List<SpecGroup> queryGroupWithParam(Long cid) {
        List<SpecGroup> groups = this.querySpecGroups(cid);
        groups.forEach(group -> group.setParams(this.querySpecParam(group.getId(), null, null, null)));
        return groups;
    }
}
